$(function() {
	$("#left").load("../common/gover-left.html");
	var userId = sessionStorage.getItem("userId");
	var applyId = parseQueryString().applyId;
	var fromSpace = isEmpty(parseQueryString().fromSpace) ? '0' : parseQueryString().fromSpace;
	var Imgs = sessionStorage.getItem("ImgItems");
	var isShow = false;
	var full = false;
	var pic = false;
	var init = false;
	var code = parseQueryString().code;
	$(".ystep1").loadStep({
		size: "small",
		color: "green",
		steps: [{
				title: "事项选择"
			},
			{
				title: "服务选择"
			},
			{
				title: "信息录入"
			},
			{
				title: "寄件选择"
			},
			{
				title: "取件选择"
			},
			{
				title: "提交成功"
			}
		]
	});
	$(".ystep1").setStep(3);
	my_vue = new Vue({
		el: '#vue_info',
		data: {
			titleName:sessionStorage.getItem('titleName'),
			uuuId: '',
			//itemsList:new Array(list.length),
			itemsList: [],
			ImgItems: {
				'0': ''
			},
		},
		methods: {
			func: function(index) {
				if(typeof this.itemsList[index] != 'array' || this.itemsList[index].length <= 0) {
					this.itemsList[index] = [];
				}
				this.itemsList[index].push("url");

			},

			goBack: function() {
				if(fromSpace != '1') {
					sessionStorage.setItem("ImgItems", toJson(this.ImgItems));
					history.go(-1);
				} else {
					$.showAlert("信息已提交,无法修改!");
				}
			},
			checkPic: function() {
			/*	if(!pic) {
					$.showConfirm({
						'title': '',
						'content': '您还没有拍摄过材料是否确认提交？',
						"showClose": false,
						"sureLabel": "去拍摄",
						"cancelLabel": "确定",
						'cancel': this.checkPicType
					});
				} else {*/
					$.showConfirm({
						'title': '',
						'content': '是否确认提交？',
						"showClose": false,
						"sureLabel": "取消",
						"cancelLabel": "确定",
						'cancel': this.checkPicType
					});
				//}
			},
			checkPicType: function() {
				sessionStorage.setItem("ImgItems", toJson(this.ImgItems));
				window.location = "detail.html?fromSpace=" + fromSpace + "&applyId=" + applyId;
			},
			additem: function(uuuid) {
				this.uuuId = uuuid;
				$(".photo-layer").animate({
					left: '0px'
				}, 500);
				isShow = true;
				full = false;
				this.pictureList(uuuid);
				this.preview(uuuid);
			},
			finishPhoto: function() {
				$(".photo-layer").animate({
					left: '980px'
				}, 500);
				isShow = false;
				this.getCailiaos();
			},
			//点击拍摄照片
			photoBtn: function() {
				if(!full) {
					this.scan();
					$(".photo-btn").hide();
					window.setTimeout(function() {
						$(".photo-btn").show();
					}, 2000);
				} else {
					$.showAlert("材料最多可拍12张");
				}
			},
			//点击重新拍摄
			fBtn: function() {
				$(".fbtn").removeClass("m_scole_show");
				$(".fbtn").addClass("m_scole_none");
				$(".photo-btn").removeClass("m_scole_none");
				$(".photo-btn").addClass("m_scole_show");
			},
			userPhoto: function() {
				$(".fbtn").removeClass("m_scole_show");
				$(".fbtn").addClass("m_scole_none");
				$(".photo-btn").removeClass("m_scole_none");
				$(".photo-btn").addClass("m_scole_show");
			},
			getCailiaos: function() {
				this.$http.get(gover.getCailiaos, {
					applyId: applyId
				}).then(function(result) {
					console.log(toJson(result.data));
					if(result.data.success) {
						this.itemsList = result.data.data;
						//+"_"++"_"+this.ImgItems.length<1)
						console.log("是不是空" + (this.ImgItems[0]));
						if(isNotEmpty(Imgs) && !init) {
							init = true;
							this.ImgItems = toObj(Imgs);
							
						}
						if(this.itemsList.length <= 0) {
							$.showAlert({"content":"此办件无需拍摄材料!","sure":this.checkPicType});
						} else {
							if(isEmpty(this.ImgItems[0])) {
								for(var i = 0; i < result.data.data.length; i++) {
									var list = result.data.data[i].uploadDetailId;
									this.ImgItems[0] = '1';
									var key = result.data.data[i].uuid;
									this.ImgItems[key] = list;
									var key = result.data.data[i].uuid;
									this.ImgItems[key] = list;

								}
							}
						}
					}
				}, function() {
					console.log('保存接口失败:' + toJson(result));

				})
			},
			deleteAll: function() {
				$.showConfirm({
					'title': '',
					'content': '是否确认全部重拍？',
					"showClose": false,
					"sureLabel": "取消",
					"cancelLabel": "确定",
					'cancel': this.deleteFiles
				});
			},
			deleteFiles: function() {
				console.log(this.uuuId + "--" + applyId);
				this.$http.post(gover.deleteFiles, {
					fromSpace: fromSpace,
					uuuId: this.uuuId,
					applyId: applyId
				}, {
					emulateJSON: true
				}).then(function(result) {
					if(result.data.success) {
						this.ImgItems[this.uuuId] = [];
						$("#picUl").html("");
					} else {
						console.log(result.data.message);
					}
				}, function() {
					console.log('保存接口失败:' + toJson(result));
				})
			},
			pictureList: function(uuuid) {
				$("#picUl").html("");
				if(isNotEmpty(uuuid) && isNotEmpty(this.ImgItems[uuuid])) {
					var html = '';
					for(var i = 0; i < this.ImgItems[uuuid].length; i++) {
						pic = true;
						var img = this.ImgItems[uuuid][i];
						html += '<div class="item"><a href="javascript:void(0);" ><img src="' + img + '" width="100%" height="100%" border="0" alt=""/></a><div class="photoindex">' + (i + 1) + '</div></div>'
					}
					$("#picUl").append(html);
				}
			},
			preview: function() {
				var time = new Date().getMilliseconds();
				$.ajax({
					url: device.preview + '?time=' + time,
					async: false,
					type: 'get',
					dataType: 'json',
					success: function(result) {
						if(result.success) {
							document.getElementById("previewImg").src = device.previewImg+"?time=" + time;
						} else {
							//showRedMsg('',result.message);
						}
						if(isShow) {
							window.setTimeout(function() {
								my_vue.preview()
							}, 2000);
						}
						return;
					},
					error: function() {
						showRedMsg('', '高拍仪暂时无法使用,请稍后再试!');
						return false;
					}
				});
			},
			scan: function() {
				var time = new Date().getTime();
				var imgName = applyId + "_" + this.uuuId + "_" + time;
				this.$http.post(device.scan, {
					name: imgName
				}, {
					emulateJSON: true
				}).then(function(result) {
					if(result.data.success) {
						var url = baseUrl3 +'/'+ result.data.data.url;

						this.ImgItems[this.uuuId].push(url);

						$("#finish-btn").focus();
						if(this.ImgItems[this.uuuId].length >= 12) {
							full = true;
						} else {
							full = false;
						}
						var i = this.ImgItems[this.uuuId].length;

						var html = '<div class="item"><a href="javascript:void(0);" ><img src="' + url + '" width="100%" height="100%" border="0" alt=""/></a><div class="photoindex">' + (i) + '</div></div>'
						$("#picUl").append(html);
						this.send(imgName);
					}
				}, function() {
					showRedMsg("","拍摄失败,请稍后重试!");
				})
			},
			//上传到服务器
			send: function(imgName) {
				console.log(imgName);
				this.$http.post(device.send, {
					fileName: imgName
				}, {
					emulateJSON: true
				}).then(function(result) {

				}, function() {
					console.log('上传失败:' + toJson(result));
				})
			}
		},
		ready: function() {
			//初始化获得总页面数和列表数据
			this.getCailiaos();
			var url = "../up/information.html?code="+code;
			if(sessionStorage.getItem('isFace') != 1) {
				 
			}
		}
	});

});